# Maintainer: Bruno Pagani <archange@archlinux.org>

pkgname=kresus
pkgver=0.18.1
pkgrel=1
pkgdesc="Self-hosted personal finance manager"
arch=(loong64 x86_64)
url="https://kresus.org"
license=(MIT)
backup=('etc/webapps/kresus/config.ini')
depends=(python nodejs-lts-gallium woob)
makedepends=(yarn libpng node-gyp)
optdepends=('python-pdfminer: La Banque Postale support')
source=(https://framagit.org/kresusapp/kresus/-/archive/${pkgver}/${pkgname}-${pkgver}.tar.bz2
        config.ini
        ${pkgname}.service
        ${pkgname}.sysusers
        ${pkgname}.tmpfiles)
sha256sums=('3a32a3c78a3b724fd186ad212e743894872a0a7aee6efa8f02c454a76973e1ed'
            '2a1de56c469b9a8e899614e6cb6ff8d6f205b5df8d30141230f1d0bc2bf15f40'
            '14d1373b0ba27b5695d75119af066348328be4104028d4d7a981252f7c9b1265'
            'd9d30f5470c7165e4917487b69d7ab82e463da4e1355056e1035ee501d3f1adc'
            'ba8ad7d9eb5d2b47fde5f6a3ab98596e5c679141b78d76d54b44830604b67632')

prepare() {
    cd ${pkgname}-${pkgver}
    yarn install --no-fund
}

build() {
    cd ${pkgname}-${pkgver}
    export CFLAGS+=" -ffat-lto-objects"
    yarn run build:prod --offline
}

package() {
    cd ${pkgname}-${pkgver}
    # Remove building modules and install production ones only
    rm -rf node_modules
    yarn install --production --no-fund
    # Move to install point
    install -d "${pkgdir}"/usr/{bin,lib/kresus/}
    mv bin build node_modules "${pkgdir}"/usr/lib/kresus/
    # Symlink executable
    ln -s /usr/lib/kresus/bin/kresus.js "${pkgdir}"/usr/bin/kresus

    # Remove useless stuff
    find "${pkgdir}"/usr/lib/kresus/ -type d \( -name 'test' -o -name 'tests' -o -name 'tools' -o -name '\.github' \) -exec rm -rv {} \+
    find "${pkgdir}"/usr/lib/kresus/ -type f \( -name '*\.md' -o -name '*\.txt' -o -name 'AUTHORS' -o -iname 'LICENSE*' -o -name 'Dockerfile' -o -name 'Makefile' -o -iname 'README*' -o -name '\.*' \) -delete -print
    # No -o -name 'package.json' because they seem required
    # node-expat artifacts
    rm -rv "${pkgdir}"/usr/lib/kresus/node_modules/node-expat/{build/{Release/{.deps,obj.target,libexpat.a},binding.Makefile,deps,node_expat.target.mk},deps,node-expat.cc}

    # Fix permissions
    chmod -R go-w "${pkgdir}"/usr/lib/kresus
    chown -R root:root "${pkgdir}"/usr/lib/kresus

    install -Dm644 LICENSE -t "${pkgdir}"/usr/share/licenses/${pkgname}/

    cd ..
    install -Dm600 config.ini -t "${pkgdir}"/etc/webapps/kresus/
    install -Dm644 ${pkgname}.service -t "${pkgdir}"/usr/lib/systemd/system/
    install -Dm644 ${pkgname}.sysusers "${pkgdir}"/usr/lib/sysusers.d/${pkgname}.conf
    install -Dm644 ${pkgname}.tmpfiles "${pkgdir}"/usr/lib/tmpfiles.d/${pkgname}.conf
}
